//
//  AppManager.swift
//  mmda
//
//  Created by 李陆涛 on 2018/4/16.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

class AppManager: NSObject {

    static let share = AppManager()
    
    var cLocation:CLLocation?
    var platform:String?
    
    private override init() {}
    
    var secret:String? {
    
        get {

            return UserDefaultManager.share.getSecret()
        }
        
        set {
            
            UserDefaultManager.share.setSecret(secret: newValue)
        }
    }
    
    var mobile:String? {
     
        get {
            
            return UserDefaultManager.share.getMobile()
        }
        
        set {
            
            UserDefaultManager.share.setMobile(mobile: newValue)
        }
    }

    var code:String? {
        
        get {
            
            return UserDefaultManager.share.getCode(mobile: self.mobile!)
        }
        
        set {
            
            UserDefaultManager.share.setCode(mobile: self.mobile!, code: newValue)
        }
    }
    
    var hasAgree:Bool {
        
        get {
            
            return UserDefaultManager.share.hasAgree()
        }
        
        set {
            
            UserDefaultManager.share.setHasAgree(argee: newValue)
        }
    }
    
    

    var token:String?
    
    
//   服务器系统时间
    private var _time:Int = 0
    
    var serverTime:Int {
        
        set(time) {
            
            if time == 0 {
                
                return
            }
            let localTime = Int(Date().timeIntervalSince1970)
            
            _time = time - localTime
        }
        get {
            
            return Int(Date().timeIntervalSince1970) + _time
        }
    }
    
    
//    appModel
    
    var appModel: AppModel?
    
    func getUnLoginToken() -> String? {
        
        guard let secret = self.secret else { return nil }
        
        guard let secret1 = secret.base64Decode()?.md5 else {return nil}
        
        let secret2 = secret1.subString(from: 11, length: 18) + secret1.subString(from: 6, length: 10) + secret1.subString(from: 9, length: 15)
        
        let time1: Int = self.serverTime
        
        let time2: Int = time1 + 180
        
        let sign = secret2 + String(time1) + String(time2) + "ok"
        
        let token = sign.md5 + String(time1) + String(time2) + "ok"
        
        return token
    }
    
//     user
    var user:User?
    
    
}
